<template>
  <div class="mod-stressFile">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.caseId"
          placeholder="用例ID"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <!--        <el-button
          type="primary"
          @click="addOrUpdateHandle()"
        >配置
        </el-button
        >-->
        <el-button type="primary" @click="stopAll()">全部停止 </el-button>
        <el-button
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除
        </el-button>
        <!-- <el-button
          type="primary"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >停止
        </el-button> -->
        
     
        <!--        <el-button-->
        <!--          type="primary"-->
        <!--          @click="addOrUpdateHandle()"-->
        <!--        >加入任务-->
        <!--        </el-button-->
        <!--        >-->
        <!--        <el-button-->
        <!--          type="primary"-->
        <!--          @click="addOrUpdateHandle()"-->
        <!--        >查看任务-->
        <!--        </el-button-->
        <!--        >-->
        <!--        <el-button-->
        <!--          type="primary"-->
        <!--          @click="addOrUpdateHandle()"-->
        <!--        >线程组-->
        <!--        </el-button-->
        <!--        >-->
        <el-button v-show="isShow" type="primary" @click="back()"
          >返回
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="fileId"
        header-align="center"
        align="center"
        width="80"
        label="文件ID"
      >
      </el-table-column>
      <el-table-column
        prop="caseName"
        header-align="center"
        align="center"
        label="用例名称"
      >
      </el-table-column>
      <el-table-column
        prop="originName"
        header-align="center"
        align="center"
        label="文件名称"
      >
        <template slot-scope="scope">
          <router-link
            :to="{ name: 'test-report', query: { fileId: scope.row.fileId } }"
            style="color: #0bb2d4"
            >{{ scope.row.originName }}
          </router-link>
        </template>
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        label="添加时间"
      >
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" size="small">创建成功</el-tag>
          <el-tag v-if="scope.row.status === 1" size="small" type="warning"
            >正在执行</el-tag
          >
          <el-tag v-if="scope.row.status === 2" size="small" type="success">
            {{ isbuttonName(scope.row.originName) }}
          </el-tag>
          <el-tag v-if="scope.row.status === 3" size="small" type="danger"
            >执行失败</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="reportStatus"
        header-align="center"
        align="center"
        width="100"
        label="测试报告"
      >
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.reportStatus === 0"
            size="small"
            type="success"
            >启动</el-tag
          >
          <el-tag v-else size="small">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        label="操作"
        width="280"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="enableHandle(scope.row.fileId)"
            v-show="isButtonShow(scope.row.originName)"
            >启动
          </el-button>
          <el-button
            type="text"
            size="small"
            v-show="isButtonShow(scope.row.originName)"
            @click="edit(scope.row.fileName)"
            >编辑
          </el-button>
          <el-button
            type="text"
            size="small"
            v-show="!isButtonShow(scope.row.originName)"
            @click="synchronizeFile(scope.row.fileId)"
            >同步
          </el-button>
          <el-link :underline="false" style="margin-left: 15px" target="_blank">
            <el-button
              type="text"
              size="small"
              icon="el-icon-download"
              @click="downloadReport(scope.row.fileId)"
            >
              下载
            </el-button>
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!--     节点信息组件-->
    <slave
      v-if="enableVisible"
      ref="enable"
      @refreshDataList="getDataList"
    ></slave>
    <!--  编辑页面组件-->
    <edit v-if="editVisible" ref="edit" @refreshDataList="getDataList"></edit>
  </div>
</template>

<script>
import slave from "./stressFile-slave";
import edit from "./stressFIle-update";

export default {
  data() {
    return {
      dataForm: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      enableVisible: false,
      slaves: false,
      isShow: false,
      editVisible: false,
    };
  },
  components: {
    slave,
    edit,
  },

  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/test/stressFile/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          caseId: this.dataForm.caseId,
          order:'asc'
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    //启动
    enableHandle(fileId) {
      this.enableVisible = true;
      this.$nextTick(() => {
        this.$refs.enable.init(fileId);
      });
    },
    // 删除
    deleteHandle(fileId) {
      var fileIds = fileId
        ? [fileId]
        : this.dataListSelections.map((item) => {
            return item.fileId;
          });
      this.$confirm(
        `确定对[fileId=${fileIds.join(",")}]进行[${
          fileId ? "删除" : "批量删除"
        }]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/test/stressFile/delete"),
            method: "post",
            data: this.$http.adornData(fileIds, false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 全部停止
    stopAll(ids) {
      this.$http({
        url: this.$http.adornUrl("/test/stressFile/stopAll"),
        method: "post",
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "停止成功",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.getDataList();
            },
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    // 强制停止
    //
    //下载文件 以文件流的方式，下载文件
    downloadReport(fileId) {
      this.$http({
        url: this.$http.adornUrl("/test/stressFile/downloadFile/" + fileId),
        headers: {
          "Content-Type": "application/octet-stream;charset=utf-8",
        },
        method: "get",
        responseType: "blob",
      })
        .then((res) => {
          const fileName = res.headers["content-disposition"].replace(
            /\w+;filename=(.*)/,
            "$1"
          );
          console.log(fileName);
          let url = window.URL.createObjectURL(new Blob([res.data]));
          let a = document.createElement("a");
          a.style.display = "none";
          a.href = url;
          a.setAttribute("download", fileName);
          document.body.appendChild(a);
          a.click(); //执行下载
          window.URL.revokeObjectURL(a.href);
          document.body.removeChild(a);
          this.$message({
            message: "下载成功！",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.getDataList();
            },
          });
        })
        .catch((error) => {
          console.log(error);
        });
    },
    //同步按钮
    synchronizeFile(fileId) {
      let fileIds = [fileId];
      this.$http({
        url: this.$http.adornUrl("/test/stressFile/synchronizeFile"),
        method: "post",
        data: this.$http.adornData(fileIds, false),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "同步成功！",
            type: "success",
            duration: 1500,
            onClose: () => {
              this.getDataList();
            },
          });
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    // 过滤器，显示文件操作按钮
    isButtonShow(originName) {
      if (originName.split(".")[1] === "jmx") {
        return true;
      } else {
        return false;
      }
    },
    //返回按钮
    back() {
      this.$router.go(-1); //返回上一层
    },
    //按钮显示名称
    isbuttonName(originName) {
      let type = originName.split(".")[1];
      console.log(type);
      if (type === "csv") {
        // this.buttonName = '同步成功'
        return "同步成功";
      } else {
        return "执行成功";
      }
    },
    edit(fileName) {
      this.editVisible = true;
      this.$nextTick(() => {
        this.$refs.edit.init(fileName);
      });
    },
  },
  created() {
    // console.log(this.$route.query.caseId);
    // this.dataForm.caseId = this.$route.query.caseId
    //设置路由跳转
    // this.$set(this.dataForm, "caseId", this.$route.query.caseId);
    // console.log(this.dataForm.caseId)
    this.getDataList()
  },
  //监听
  watch: {
    $route(now, old) {
      //监控路由变换，控制返回按钮的显示
      if (now.path == "/home") {
        this.isShow = false;
      } else {
        // 监听的上一次路由的值,从路由的值中,初始化传的参数,放到监听里面,是为了让页面不停的跳转使用,
        // 不建议放到created中,因为created 只加载使用一次
        console.log('路由的caseid',this.$route.query.caseId);
        this.dataForm.caseId = this.$route.query.caseId
        console.log('路由的caseid',this.dataForm.caseId)
        this.isShow = true;
      }
    },
  },
};
</script>
